import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'

const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000
})
request.interceptors.request.use(config => {
  const token = store.state.user.token
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, error => {
  return Promise.reject(error)
})

request.interceptors.response.use(result => {
  if (result.data.code === 200 || result.data.type === 'image/png') {
    return result.data
  } else {
    Message.error(result.data.msg)
    return Promise.reject(result.data.msg)
  }
}, error => {
  return Promise.reject(error)
})
export default request
